package com.app.fk.mensa;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseReadWriter {

	private DatabaseHelper databaseHelper;
	private mensa context;
	
	DatabaseReadWriter(mensa context)
	{
		this.context = context;
		databaseHelper = new DatabaseHelper(context);
	}
	
	private void postInfoToLoader(int value,String info)
	{
		if(context.progDailog != null)
			{
			context.progDailog.incrementProgressBy(value);
			context.progDailog.setMessage(info);
			}
	}
	
	 public static void writeMenues(Context context, int week, Menues menues) 
		{
		 	DatabaseHelper databaseHelper = new DatabaseHelper(context);
			SQLiteDatabase db = databaseHelper.getWritableDatabase();
			
			ContentValues cv = new ContentValues();
			
			cv.put(DatabaseHelper.WOCHE, week);
			cv.put(DatabaseHelper.MONTAG_MENUE_1, menues.montag_1);
			cv.put(DatabaseHelper.MONTAG_MENUE_2, menues.montag_2);
			cv.put(DatabaseHelper.MONTAG_MENUE_3, menues.montag_3);
			cv.put(DatabaseHelper.DIENSTAG_MENUE_1, menues.dienstag_1);
			cv.put(DatabaseHelper.DIENSTAG_MENUE_2, menues.dienstag_2);
			cv.put(DatabaseHelper.DIENSTAG_MENUE_3, menues.dienstag_3);
			cv.put(DatabaseHelper.MITTWOCH_MENUE_1, menues.mittwoch_1);
			cv.put(DatabaseHelper.MITTWOCH_MENUE_2, menues.mittwoch_2);
			cv.put(DatabaseHelper.MITTWOCH_MENUE_3, menues.mittwoch_3);
			cv.put(DatabaseHelper.DONNERSTAG_MENUE_1, menues.donnerstag_1);
			cv.put(DatabaseHelper.DONNERSTAG_MENUE_2, menues.donnerstag_2);
			cv.put(DatabaseHelper.DONNERSTAG_MENUE_3, menues.donnerstag_3);
			cv.put(DatabaseHelper.FREITAG_MENUE_1, menues.freitag_1);
			cv.put(DatabaseHelper.FREITAG_MENUE_2, menues.freitag_2);
			cv.put(DatabaseHelper.FREITAG_MENUE_3, menues.freitag_3);
		
			int deleted = db.delete(DatabaseHelper.TABLE_NAME, "week = "+ week, null);
			
			db.insert(DatabaseHelper.TABLE_NAME, DatabaseHelper.WOCHE, cv);
			db.close();
			
		}
	 public static int deleteDatabase(Context context)
	 {
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		int deleted = db.delete(DatabaseHelper.TABLE_NAME, null , null);
		db.close();
		return deleted;
	 }
	 
	 public static Menues readMenues(Context context, int week) 
		{
		 	DatabaseHelper databaseHelper = new DatabaseHelper(context);
			SQLiteDatabase db = databaseHelper.getReadableDatabase();

			Cursor c = db.query(DatabaseHelper.TABLE_NAME, null , "week ="+ week, null, null, null, null);
			int wocheColumn = c.getColumnIndex(DatabaseHelper.MONTAG_MENUE_1);
			
			if(c != null) 
				{
				c.moveToFirst();
				Menues menue = new Menues();
				String[] arr = new String [15];
				
				for(int i = wocheColumn; i < wocheColumn+15; i++)
				{
					if(c.isNull(i))
						return null;
					
					arr[i-wocheColumn] = c.getString(i);			
				}
				c.close();
				db.close();			
				if(menue.setMenues(arr)!= false)
					{
					return menue;
					}
				}	
	    	return null;
		}
	 
	 public static boolean isWeekInDatabase(Context context, int week)
	    {
		 	DatabaseHelper databaseHelper = new DatabaseHelper(context);
			SQLiteDatabase db = databaseHelper.getReadableDatabase();
			
			Cursor c = db.query("menues", null , "week ="+ week, null, null, null, null);
			
			if(c.getCount() == 0)
				{
				c.close();
				db.close();
				return false;
				}
			else 
				{
				c.close();
				db.close();
				return true;
				}
	  	
	    }
}
